package com.ruoyi.common.core.utils;

import java.util.regex.Pattern;

public class ChineseNameUtils {

    // 中文Unicode范围
    private static final Pattern CHINESE_CHAR_PATTERN = Pattern.compile("[\u4e00-\u9fa5]");
    private static final Pattern CHINESE_NAME_PATTERN = Pattern.compile("^[\u4e00-\u9fa5·]{2,10}$");

    /**
     * 综合验证中文名字
     */
    public static ValidationResult validateChineseName(String name) {
        if (name == null) {
            return new ValidationResult(false, "姓名不能为空");
        }

        String trimmedName = name.trim();

        if (trimmedName.isEmpty()) {
            return new ValidationResult(false, "姓名不能为空");
        }

        // 基本格式检查
        if (!CHINESE_NAME_PATTERN.matcher(trimmedName).matches()) {
            return new ValidationResult(false, "姓名只能包含中文汉字和间隔符·");
        }

        // 长度检查
        int chineseCharCount = countChineseCharacters(trimmedName);
        if (chineseCharCount < 2 || chineseCharCount > 8) {
            return new ValidationResult(false, "姓名长度应为2-8个汉字");
        }

        // 特殊字符检查（不能只有间隔符）
        if (trimmedName.replace("·", "").isEmpty()) {
            return new ValidationResult(false, "姓名不能只包含间隔符");
        }

        // 间隔符位置检查
        if (trimmedName.startsWith("·") || trimmedName.endsWith("·")) {
            return new ValidationResult(false, "间隔符不能在开头或结尾");
        }

        // 连续间隔符检查
        if (trimmedName.contains("··")) {
            return new ValidationResult(false, "不能有连续间隔符");
        }

        return new ValidationResult(true, "验证通过");
    }

    /**
     * 统计字符串中的中文字符数量
     */
    private static int countChineseCharacters(String str) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (CHINESE_CHAR_PATTERN.matcher(String.valueOf(str.charAt(i))).matches()) {
                count++;
            }
        }
        return count;
    }

    /**
     * 简单验证（性能较好）
     */
    public static boolean isSimpleChineseName(String name) {
        if (name == null || name.trim().isEmpty()) {
            return false;
        }
        String trimmedName = name.trim();
        return trimmedName.matches("^[\u4e00-\u9fa5]{2,8}$") ||
                trimmedName.matches("^[\u4e00-\u9fa5]{2,4}·[\u4e00-\u9fa5]{2,4}$");
    }

    /**
     * 验证结果类
     */
    public static class ValidationResult {
        private final boolean valid;
        private final String message;

        public ValidationResult(boolean valid, String message) {
            this.valid = valid;
            this.message = message;
        }

        public boolean isValid() {
            return valid;
        }

        public String getMessage() {
            return message;
        }
    }


    public static void main(String[] args) {
        // 测试用例
        String[] testNames = {
                "张三", "李四", "欧阳明日", "阿凡提·买买提",
                "A张三", "张", "张三李四王五", "张·", "·李", "张··李"
        };

        for (String name : testNames) {
            ChineseNameUtils.ValidationResult result = ChineseNameUtils.validateChineseName(name);
            System.out.printf("姓名: %-10s 验证结果: %-5s 说明: %s%n",
                    name, result.isValid() ? "通过" : "失败", result.getMessage());
        }

        // 简单验证测试
        System.out.println("\n=== 简单验证 ===");
        for (String name : testNames) {
            boolean isValid = ChineseNameUtils.isSimpleChineseName(name);
            System.out.printf("姓名: %-10s 简单验证: %s%n", name, isValid);
        }
    }
}